import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
import { getArticleList } from "@/api/article";


// 创建异步action
export const getArticles = createAsyncThunk("article/getArticles", async (params) => {
  // console.log(params,"action参数");
  const res = await getArticleList(params);
  console.log("articleRes",res)
  return res;
});

const stuSlice = createSlice({
  name: "article",
  initialState: {
    searchParam: {
      title: null,
      tagId:null,
      typeId: null,
      status: null,
    },
    articleList: [],
    total:0,
    currentPage:1,
    pageSize:5,
  },
  reducers: {
    setSearch(state, action) {
      console.log(action.payload,"search参数变化")
      state.searchParam = action.payload;
    }
  },
  // 可以处理别处定义的actions
  extraReducers(builder) {
    builder
    .addCase(getArticles.pending, (state) => {
      console.log("🚀 ~ 进行中！")
    })
    .addCase(getArticles.fulfilled, (state, {payload}) => {
      console.log("fulfilled",payload);
      state.articleList = payload.data;
      state.total = payload.total;
      state.currentPage = payload.currentPage;
      state.pageSize = payload.pageSize;
    })
    .addCase(getArticles.rejected, (state, err) => {
      console.log("🚀 ~ rejected", err)
    });
  }
});


console.log(stuSlice.actions, "stuSlice-actions");

const { setSearch } = stuSlice.actions;
export { setSearch };

export default stuSlice;
